package homework.ch11_13.p4;


public class CompositeComponent extends Component{

    protected ComponentList childs=new ComponentList();
    private Object UnsupportedOperationException;

    public CompositeComponent() {
    }

    public CompositeComponent(int id, String name, double price) {
        super(id, name, price);
    }

    public void add(Component component)throws UnsupportedOperationException{
        childs.add(component);
        calcPrice();
    }


    public double calcPrice() {
        double p=0;
        for(Component child:childs){
            p=p+child.getPrice();
        }
        this.setPrice(p);
        return p;
    }

    public CompositeIterator iterator(){
      CompositeIterator s=new CompositeIterator(childs);
      return s;
    }

    public String toString() {
        String indent = "";

        StringBuffer buf = new StringBuffer();
        buf.append("id: " + id + ", name: " + name + ", price: " + getPrice()).append("\n");
        if(childs.size() > 0){
            buf.append(indent + "sub-components of " + name + ":").append("\n");
            for (Component c: childs){
                buf.append(c.toString()).append("\n");
            }
        }

        return buf.toString();
    }

    public void remove(Component component)throws UnsupportedOperationException{
        childs.remove(component);
    }

}
